package com.ganjingcun.roma.persist.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by GanJc on 2016/4/16.
 */
public final class SessionUtil {

    private static SqlSessionFactory sqlSessionFactory ;

    public static SqlSession getSession(){
        if(sqlSessionFactory==null){
            InputStream inputStream = null;
            try {
                inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        }
        return sqlSessionFactory.openSession();
    }

}
